package com.yikes.srb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.utils.object.BeanUtils;
import com.yikes.srb.leadingModle.form.srbPermitFile.SrbPermitFileAddForm;
import com.yikes.srb.leadingModle.form.srbPermitFile.SrbPermitFileEditForm;
import com.yikes.srb.leadingModle.form.srbPermitFile.SrbPermitFileQuery;
import com.yikes.srb.leadingModle.vo.SrbPermitFileVO;
import com.yikes.srb.pojo.entity.SrbPermitFile;
import com.yikes.srb.mapper.SrbPermitFileMapper;
import com.yikes.srb.service.SrbPermitFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * yikes上传附件表 服务实现类
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SrbPermitFileServiceImpl extends ServiceImpl<SrbPermitFileMapper, SrbPermitFile> implements SrbPermitFileService {

    @Override
    public PageResult<SrbPermitFileVO> listPage(SrbPermitFileQuery query) {

        Page<SrbPermitFile> page = lambdaQuery()
                .eq(ObjectUtil.isNotNull(query.getRelationId()), SrbPermitFile::getRelationId, query.getRelationId())
                .like(StrUtil.isNotBlank(query.getFileType()), SrbPermitFile::getFileType, query.getFileType())
                .like(StrUtil.isNotBlank(query.getLabelName()), SrbPermitFile::getLabelName, query.getLabelName())
                .like(StrUtil.isNotBlank(query.getFileName()), SrbPermitFile::getFileName, query.getFileName())
                .like(StrUtil.isNotBlank(query.getType()), SrbPermitFile::getType, query.getType())
                .page(Page.of(query.getPageNum(), query.getPageSize()));

        List<SrbPermitFileVO> bean = BeanUtils.toBean(page.getRecords(), SrbPermitFileVO.class);
        return PageResult.build(page.getTotal(), page.getPages(), bean);
    }

    @Override
    public SrbPermitFileVO infoId(Long id) {

        SrbPermitFile srbPermitFile = getById(id);
        SrbPermitFileVO bean = BeanUtils.toBean(srbPermitFile, SrbPermitFileVO.class);
        return bean;
    }

    @Override
    public void create(SrbPermitFileAddForm form) {

        SrbPermitFile bean = BeanUtils.toBean(form, SrbPermitFile.class);
        save(bean);

    }

    @Override
    public void edit(SrbPermitFileEditForm form) {

        SrbPermitFile bean = BeanUtils.toBean(form, SrbPermitFile.class);
        updateById(bean);
    }

    @Override
    public void del(Long id) {
        baseMapper.deleteById(id);
    }
}
